package com.qys.training.server.controller;


import com.qys.training.base.config.Font;
import com.qys.training.base.config.PDFTextConfig;
import com.qys.training.base.dto.BaseResult;
import com.qys.training.biz.auth.service.UserService;
import com.qys.training.biz.file.criteria.FileInfoCriteria;
import com.qys.training.biz.file.service.FileInfoService;
import jdk.internal.dynalink.linker.LinkerServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/file")
public class FileInfoController {

    public static final Logger logger = LoggerFactory.getLogger(FileInfoController.class);

    @Autowired
    private FileInfoService fileInfoService;

    /**
     * 上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public BaseResult create(@RequestParam("file") MultipartFile file) {
        Long id = fileInfoService.create(file);
        return BaseResult.success(id);
    }

    /**
     * 更新文件
     * @param file
     * @param id
     * @return
     */
    @PostMapping("/update")
    public BaseResult update(@RequestParam("file") MultipartFile file, @RequestParam("id")Long id) {
        fileInfoService.update(id, file);
        return BaseResult.success(id);
    }


    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public @ResponseBody
    BaseResult delete(@PathVariable Long id) {
        fileInfoService.delete(id);
        return BaseResult.success();
    }

    /**
     * 根据ID下载
     * @param response
     * @param id
     * @return
     */
    @GetMapping("/download/{id}")
    public @ResponseBody
    void download(HttpServletRequest request,HttpServletResponse response, @PathVariable Long id) {
        fileInfoService.downloadById(request,response,id);
    }

    /**
     * 查看列表
     * @param criteria
     * @return
     */
    @GetMapping("/getList")
    public @ResponseBody
    BaseResult getPage(FileInfoCriteria criteria) {
        Map map = fileInfoService.findList(criteria);
        return BaseResult.success(map);
    }

    /**
     * 多文件压缩下载
     * @param request
     * @param response
     * @param ids
     * @throws IOException
     */
    @GetMapping("/getListByIds")
    public @ResponseBody
    void getListByIds(HttpServletRequest request,HttpServletResponse response,@RequestParam List<Long> ids) throws IOException {
        fileInfoService.downloadZip(request,response,ids);
    }

    /**
     * 给PDF添加文字
     * @param id
     * @param text
     * @param x
     * @param y
     * @param font
     * @return
     */
    @PostMapping("/addText")
    public @ResponseBody
    BaseResult addText(@RequestParam("id") Long id, @RequestParam("text")String text, @RequestParam("x")float x, @RequestParam("y")float y, @RequestParam("font")Font font){
        fileInfoService.addText(id,text,x,y,font);
        return BaseResult.success();
    }

}
